$(function () {
    //Date picker
    $('.input-datetime').datepicker({
        format: "yyyy-mm-dd",
        language: "zh-CN",
        autoclose: true
    });

    // 搜索查询
    $('#search-form').submit(function () {
        var param = getUrlParam();
        var data = $(this).serializeArray();
        for (var i in data) {
            if (data[i].value) {
                param[data[i].name] = data[i].value;
            }
        }
        delete param.page; // 重置分页
        window.location.href = setUrl(param);
        return false;
    });

    // 清空搜索条件
    $('#clean-search-button').click(function () {
        var param = getUrlParam();
        delete param.field;
        delete param.start_time;
        delete param.end_time;
        delete param.keyword;
        delete param.page;
        window.location.href = setUrl(param);
    });

    // RESTful 请求操作
    $('.table').find('.handle').click(function () {
        var method = $(this).attr('data-method');
        var url = $(this).attr('href');
        var content = $(this).attr('data-content');
        if (method == 'get' && !content) {
            window.location.href = url;
        } else {
            switchModal({
                url: url,
                method: method,
                content: content
            });
        }
        return false;
    });

    // 批量请求操作
    $('.multi-handle').click(function () {
        var method = $(this).attr('data-method');
        var url = $(this).attr('href');
        var content = $(this).attr('data-content');
        var checkbox = $('.switch-checkbox');
        var value = [];
        if (method == 'get' && !content) {
            window.location.href = url;
        } else {
            checkbox.each(function () {
                if (this.checked == true) value.push(this.value);
            });
            if (value.length < 1) {
                alert('请选择记录后再进行操作！');
                return false;
            }
            switchModal({
                url: url,
                method: method,
                content: content,
                data: {
                    'checkbox': value
                }
            });
        }
        return false;
    });

    // 全选，取消全选
    $('#switchCheckbox').click(function () {
        var checked = this.checked;
        var checkbox = $('.switch-checkbox');
        checkbox.each(function () {
            this.checked = checked;
        });
    });

    // 模态对话框
    function switchModal(param) {
        var modal = $('#modal-alert');
        var modal_form = modal.find('.modal-form');
        modal_form.attr('action', param.url);
        //modal_form.attr('method', method);
        var input_method = modal_form.children('.input-method');
        input_method.val(param.method);
        var modal_body = modal_form.children('.modal-body');
        var main = $('<div>');
        var content = '确定要执行操作吗?';
        if (param.content != undefined && param.content != '') {
            content = param.content;
        }
        main.append('<p>' + content + '</p>');
        if (param.data != undefined) {
            for (var key in param.data) {
                main.append('<input type="hidden" name="' + key + '" value="' + param.data[key] + '">');
            }
        }
        modal_body.html(main);
        modal.modal();
    }
});